/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package KeyServer;

import java.io.*;
import java.net.*;
import java.util.Scanner;
import mysql.MysqlHandler;

/**
 * Demo Server: Contains a multi-threaded socket server sample code.
 */
public class ServerDemo extends Thread {

    final static int _portNumber = 6000; //Arbitrary port number
    static int duration;

    public static void main(String[] args) {
        try {

           
            
            Scanner reader = new Scanner(System.in);
            System.out.println("Please enter the duration of the temporary password (in mins):  ");
            //get user input for a
            duration=reader.nextInt();
            System.out.println(duration);
                
            
            MysqlHandler mysql = new MysqlHandler();
            mysql.createConnection();

            mysql.closeConnection();

            new ServerDemo().startServer();
        } catch (Exception e) {
            System.out.println("I/O failure: " + e.getMessage());
            e.printStackTrace();
        }

    }

    public void startServer() throws Exception {
        ServerSocket serverSocket = null;
        boolean listening = true;

        try {
            serverSocket = new ServerSocket(_portNumber);
        } catch (IOException e) {
            System.err.println("Could not listen on port: " + _portNumber);
            System.exit(-1);
        }

        while (listening) {
            handleClientRequest(serverSocket);
        }

        serverSocket.close();
    }

    private void handleClientRequest(ServerSocket serverSocket) {
        try {
            new ConnectionRequestHandler(serverSocket.accept(), duration).run();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

}
